package com.gxl.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gxl.pojo.UserPageInfo;
import com.gxl.pojo.Users;
import com.gxl.service.UsersService;

/**
 * 此时不被spring扫描，由springmvc扫描
 * @author set_free
 *
 */
@Controller
public class DemoController {
	@Autowired
	private UsersService usersService;
	//文件上传测试
	@RequestMapping("/upload")
	public String upload(String username,MultipartFile myfile,HttpServletRequest req) throws IOException{
		String filepath = req.getServletContext().getRealPath("/file");
		File file = new File(filepath,myfile.getOriginalFilename());
		FileUtils.copyInputStreamToFile(myfile.getInputStream(), file);
		//springmvc中无论forward,redirect，/都是webcontent.
		return "upload";
	}
	//下载文件测试 不跳转
	@RequestMapping("/download")
	public void download(String filename,HttpServletRequest req,HttpServletResponse res) throws IOException{
		//设置响应头
		res.setHeader("Content-Disposition", "attachment;filename="+filename);
		String filepath = req.getServletContext().getRealPath("/file");
		File file = new File(filepath,filename);
		System.out.println(filename);
		ServletOutputStream  os = res.getOutputStream() ;
		//这个不能用，这个是输出字符流
//		res.getWriter();
		//把文件流输出,调用工具类中的转换方法
		os.write(FileUtils.readFileToByteArray(file));
		os.flush();
		os.close();
		
	}
	
	//返回对象json测试
	@RequestMapping("/login")//斜杠可以不写自动会添加
	@ResponseBody
	public Users demo(Map<String,Object> map,Model model,HttpServletRequest req,Users users,@RequestParam(defaultValue="10")int pageSize){
		System.out.println("users:"+users.toString());
		UserPageInfo upi = usersService.showUserList_forSpring(1,10,null);
		System.out.println("login info:"+users.toString()+"--pageSize:"+pageSize+"--"+upi.getUserList().size());
		req.setAttribute("reqval", "reqval1");
		req.getSession().setAttribute("se", "session");
		model.addAttribute("mkey", "mkey1");
		map.put("mapkey", "mapkey1");
		return users;
	}
	
	//内部跳转测试
	@RequestMapping("/login1")//斜杠可以不写自动会添加
	@ResponseBody//返回具体的内容
	public Users demo1(Map<String,Object> map,Model model,HttpServletRequest req,Users users,@RequestParam(defaultValue="10")int pageSize){
		System.out.println("users:"+users.toString());
		UserPageInfo upi = usersService.showUserList_forSpring(1,10,null);
		System.out.println("login info:"+users.toString()+"--pageSize:"+pageSize+"--"+upi.getUserList().size());
		req.setAttribute("reqval", "reqval1");
		req.getSession().setAttribute("se", "session");
		model.addAttribute("mkey", "mkey1");
		map.put("mapkey", "mapkey1");
		model.addAttribute("users",users);
		//因为配置了解析器，这么写后就用自定义的解析器。
//		return "forward:/test2";
		return users;
	}
	//内部跳转
	@RequestMapping("/test2")//斜杠可以不写自动会添加
	public String test2(){
		System.out.println("test执行");
		return "forward:/controller_index.jsp";
	}
	//登录验证
	@RequestMapping("/login2")//斜杠可以不写自动会添加
	public String login2(Model model,HttpServletRequest req,Users users){
		if(null != users){
			String name = users.getName();
			if(null != name && name.equals("gxl")){
				req.getSession().setAttribute("users", users);
				return "forward:/index.jsp";
			}
		}
		model.addAttribute("users",users);
		model.addAttribute("userinfo","我们都是中国人");
//		return "index";
		req.getSession().setAttribute("users", null);
		model.addAttribute("loginfo","登录失败");
		return "redirect:/controller_index";
	}
	//页面公共跳转
	@RequestMapping("{url}")
	public String url(@PathVariable String url){
		System.out.println("公共跳转"+url);
		return url;
	}

}
